local maigu = fk.CreateSkill {
  name = "th_maigu",
  max_phase_use_time = 1,
}

local spec = { ---@type TrigSkelSpec<fun(self: TriggerSkill, event: DyingEvent|PhaseEvent, target: ServerPlayer, player: ServerPlayer, data: DyingData|PhaseData):any>
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(maigu.name) and player:getLostHp() > 0 then
      return event == fk.EnterDying or (player.phase == Player.Finish or player.phase == Player.Start)
    end
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = maigu.name,
      prompt = "#th_maigu-invoke:::" .. player:getLostHp()
    })
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local x = player:getLostHp()
    if x > 0 then
      player:drawCards(x, maigu.name)
    end
    local n = math.max(#player:getCardIds("h") - x, 0)
    if n ~= 0 then
      room:askToDiscard(player, {
        min_num = n,
        max_num = n,
        cancelable = false,
        skill_name = maigu.name,
      })
    end
  end
}

maigu:addEffect(fk.EnterDying, spec, { check_skill_limit = true })
maigu:addEffect(fk.EventPhaseProceeding, spec, { check_skill_limit = true })

Fk:loadTranslationTable{
  ["th_maigu"] = "埋骨",
  [":th_maigu"] = "每阶段限一次，准备阶段、结束阶段或你进入濒死状态时，你可以摸X张牌并将手牌弃置至X张（X为你已损失的体力值）。",

  ["#th_maigu-invoke"] = "埋骨：你可以摸%arg张牌并将手牌弃置至%arg张",
}

return maigu